package com.dongjiantong.web.protal.controller.farmrecords;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.service.FarmStockService;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.enums.ProductionQuantityChangeEnum;
import com.dongjiantong.farmrecords.service.FarmRecordsProductionService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.enums.AnimalTypeEnum;
import com.dongjiantong.sys.model.SysArea;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.dongjiantong.sys.service.SysAreaService;
import com.dongjiantong.user.enums.FarmsEnum;
import com.dongjiantong.user.service.FarmsService;
import com.dongjiantong.user.service.OrgUserAreaService;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 养殖档案-信息总览-存栏分析
 * @author Lee
 * @date 2017/12/29
 */
@Slf4j
@OpenBean
public class StockAnalysisController {

    @Reference(version = "1.0.0")
    public OrgUserAreaService orgUserAreaService;
    @Reference(version = "1.0.0")
    public FarmsService farmsService;
    @Reference(version = "1.0.3")
    public AnimalTypeService animalTypeService;
    @Reference(version = "1.0.0")
    public FarmStockService farmStockService;
    @Reference(version = "1.0.0")
    public SysAreaService sysAreaService;
    @Reference(version = "1.0.0")
    public FarmRecordsProductionService farmRecordsProductionService;

    /**
     * 近一周变化趋势
     */

    /**
     * 按数量变化统计
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param areaList 地区code List
     * @param productType 养殖畜种
     */
    @OpenMethod(key = "web.infoAnalysis.quantityChangeStatistics",
                            paramNames = {"startDate","endDate","areaList","productType"},
                            httpMethods = RequestMethod.GET)
    public Response<Object> quantityChangeStatistics(String startDate,
                                           String endDate,
                                           List<Integer> areaList,
                                           Integer productType){
        Map result = Maps.newHashMap();//结果
        Map map = Maps.newHashMap();//条件

        //todo 基础信息
        //登陆人信息
        LoginInfo loginInfo = UserUtil.getCurrentUser();
        //规模养殖场和散养养殖场
        List<Integer> farmTypes = Lists.newArrayList(FarmsEnum.Breed.getCode(),FarmsEnum.Retail.getCode());
        map.put("userId",loginInfo.getId());
        map.put("farmTypes",farmTypes);

        //todo 开始结束日期
        if (!Strings.isNullOrEmpty(startDate)){
            Date start = new Date(Long.valueOf(startDate));
            map.put("startDate",start);
        }
        if (!Strings.isNullOrEmpty(endDate)){
            Date end = new Date(Long.valueOf(endDate));
            map.put("endDate",end);
        }

        //todo 养殖畜种
        if (productType != null){
            int len = productType.toString().length();
            if (len == 1){
                //家禽
                map.put("animalOneType",productType);
            }else {
                //家畜 种类
                map.put("animalTwoType",productType);
            }
        }

        //todo 变化种类
        List<Integer> quantityChangeType = Lists.newArrayList(ProductionQuantityChangeEnum.PRODUCTION_BIRTH.getCode(), ProductionQuantityChangeEnum.PRODUCTION_IN.getCode(), ProductionQuantityChangeEnum.PRODUCTION_OUT.getCode(), ProductionQuantityChangeEnum.PRODUCTION_DEATH.getCode());

        //todo 地区
        String areaName = null;
        if (areaList.size() > 0){
            Integer provinceId = 0,cityId = 0,regionId = 0,streetId = 0,villageId = 0;
            switch (areaList.size()) {
                case 1:
                    provinceId = areaList.get(0);
                    map.put("provinceId", provinceId);
                    break;
                case 2:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    map.put("provinceId", provinceId);
                    map.put("cityId", cityId);
                    break;
                case 3:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    regionId = areaList.get(2);
                    map.put("provinceId", provinceId);
                    map.put("cityId", cityId);
                    map.put("regionId", regionId);
                    break;
                case 4:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    regionId = areaList.get(2);
                    streetId = areaList.get(3);
                    map.put("provinceId", provinceId);
                    map.put("cityId", cityId);
                    map.put("regionId", regionId);
                    map.put("streetId", streetId);
                    break;
                case 5:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    regionId = areaList.get(2);
                    streetId = areaList.get(3);
                    villageId = areaList.get(4);
                    map.put("provinceId", provinceId);
                    map.put("cityId", cityId);
                    map.put("regionId", regionId);
                    map.put("streetId", streetId);
                    map.put("villageId", villageId);
                    break;
            }
            SysArea sysArea = sysAreaService.getAreaName(provinceId,cityId,regionId,streetId,villageId);
            if (sysArea != null){
                areaName = sysArea.getArea_name();
            }
        }else {
            //查询默认管辖区域的存栏变化
        }

        //todo 数量变化
        Map changeQuantityMap = Maps.newHashMap();
        //如果养殖畜种不为空按选择的养殖畜种查询,否则查询所有畜种的变化量
        for (Integer quantityType : quantityChangeType) {
            map.put("group",quantityType);
            Integer quantity = farmRecordsProductionService.selectQuantityChange(map);

            if (ProductionQuantityChangeEnum.PRODUCTION_BIRTH.getCode().equals(quantityType)){
                changeQuantityMap.put("birth",quantity);
            } else if (ProductionQuantityChangeEnum.PRODUCTION_IN.getCode().equals(quantityType)){
                changeQuantityMap.put("in",quantity);
            } else if (ProductionQuantityChangeEnum.PRODUCTION_OUT.getCode().equals(quantityType)){
                changeQuantityMap.put("out",quantity);
            } else if (ProductionQuantityChangeEnum.PRODUCTION_DEATH.getCode().equals(quantityType)){
                changeQuantityMap.put("death",quantity);
            }
        }

        //todo 返回结果
        result.put("areaName",areaName);
        result.put("changeQauantity",changeQuantityMap);
        return Response.ok(result);
    }

    /**
     * 根据地区查看地区的动物存栏量
     * @param areaList
     */
    @OpenMethod(key = "web.farmrecords.stockAnalysis.obtainStockNumberByArea",
            paramNames = {"areaList"},
            httpMethods = RequestMethod.GET)
    public Response<Object> obtainQuantityByArea(List<Integer> areaList){
        Map<String,Object> result = Maps.newHashMap();

        Map<String,Object> paramMap = Maps.newHashMap();

        //todo 基础数据
        LoginInfo loginInfo = UserUtil.getCurrentUser();
        //规模养殖场和散养养殖场
        List<Integer> farmTypes = Lists.newArrayList(FarmsEnum.Breed.getCode(),FarmsEnum.Retail.getCode());

        paramMap.put("userId",loginInfo.getId());
        paramMap.put("farmTypes",farmTypes);

        //todo 猪牛羊家禽
        //家畜的种类
        List<Integer> livestockAnimalType = Lists.newArrayList(AnimalTypeEnum.PIG.getCode(),AnimalTypeEnum.BULL.getCode(),AnimalTypeEnum.SHEEP.getCode());
        //家禽的种类
        Integer poultryAnimalType = AnimalTypeEnum.POULTRY.getCode();

        //todo 地区
        //地区名称
        String areaName = null;
        if (areaList.size() > 0){
            Integer provinceId = 0,cityId = 0,regionId = 0,streetId = 0,villageId = 0;
            switch (areaList.size()){
                case 1:
                    provinceId = areaList.get(0);
                    paramMap.put("provinceId",provinceId);
                    break;
                case 2:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    paramMap.put("provinceId",provinceId);
                    paramMap.put("cityId",cityId);
                    break;
                case 3:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    regionId = areaList.get(2);
                    paramMap.put("provinceId",provinceId);
                    paramMap.put("cityId",cityId);
                    paramMap.put("regionId",regionId);
                    break;
                case 4:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    regionId = areaList.get(2);
                    streetId = areaList.get(3);
                    paramMap.put("provinceId",provinceId);
                    paramMap.put("cityId",cityId);
                    paramMap.put("regionId",regionId);
                    paramMap.put("streetId",streetId);
                    break;
                case 5:
                    provinceId = areaList.get(0);
                    cityId = areaList.get(1);
                    regionId = areaList.get(2);
                    streetId = areaList.get(3);
                    villageId = areaList.get(4);
                    paramMap.put("provinceId",provinceId);
                    paramMap.put("cityId",cityId);
                    paramMap.put("regionId",regionId);
                    paramMap.put("streetId",streetId);
                    paramMap.put("villageId",villageId);
                    break;
            }
            SysArea sysArea = sysAreaService.getAreaName(provinceId,cityId,regionId,streetId,villageId);
            if (sysArea != null){
                areaName = sysArea.getArea_name();
            }
        }else {
            //查询默认管辖区域的存栏情况
        }

        Map<String,Integer> quantityMap = Maps.newHashMap();
        //todo 存栏量
        //家畜的存栏量
        for (Integer code : livestockAnimalType) {
            paramMap.put("animalTwoType",code);
            Integer quantity = farmStockService.selectTotalQuantity(paramMap);
            if (quantity == null){
                quantity = 0;
            }

            String animalName = null;
            if (AnimalTypeEnum.PIG.getCode().equals(code)){
                animalName = "pig";
            }else if (AnimalTypeEnum.BULL.getCode().equals(code)){
                animalName = "bull";
            }else if (AnimalTypeEnum.SHEEP.getCode().equals(code)){
                animalName = "sheep";
            }

            quantityMap.put(animalName,quantity);
        }

        paramMap.remove("animalTwoType");

        //家禽的存栏量
        if (poultryAnimalType != null){
            paramMap.put("animalOneType",poultryAnimalType);
            Integer poultryQuantity = farmStockService.selectTotalQuantity(paramMap);
            if (poultryQuantity == null){
                poultryQuantity = 0;
            }

            quantityMap.put("poultry",poultryQuantity);
        }

        //todo 返回地区以及地区各种类的存栏量
        result.put("areaName",areaName);
        result.put("quantity",quantityMap);
        return Response.ok(result);
    }

}
